<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    if (session.getAttribute("user") == null) {
        response.sendRedirect(request.getContextPath() + "/login.jsp");
        return;
    }
%>
<html>
<head>
    <title>员工信息管理系统 - 员工列表</title>
    <link rel="stylesheet" href="../../css/style.css">
</head>
<body>
<header>
    <div class="logo">员工信息管理系统</div>
    <nav>
        <ul>
            <li><a href="../../dashboard">首页</a></li>
            <li><a href="employees?action=list" class="active">员工管理</a></li>
        </ul>
    </nav>
    <div class="user-info">
        <span>欢迎, <%= ((com.company.ems.model.User)session.getAttribute("user")).getUsername() %></span>
        <a href="../../logout" class="logout-btn">退出登录</a>
    </div>
</header>

<main>
    <div class="container">
        <div class="header-actions">
            <h2>员工列表</h2>
            <a href="employees?action=add" class="btn success">添加员工</a>
        </div>

        <!-- 搜索表单 -->
        <div class="search-form">
            <form action="employees?action=search" method="get">
                <div class="form-row">
                    <div class="form-group">
                        <label for="name">姓名:</label>
                        <input type="text" id="name" name="name"
                               value="<%= request.getParameter("name") != null ? request.getParameter("name") : "" %>">
                    </div>
                    <div class="form-group">
                        <label for="department">部门:</label>
                        <input type="text" id="department" name="department"
                               value="<%= request.getParameter("department") != null ? request.getParameter("department") : "" %>">
                    </div>
                    <div class="form-group">
                        <label for="position">职位:</label>
                        <input type="text" id="position" name="position"
                               value="<%= request.getParameter("position") != null ? request.getParameter("position") : "" %>">
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn primary">搜索</button>
                        <a href="employees?action=list" class="btn secondary">重置</a>
                    </div>
                </div>
            </form>
        </div>

        <!-- 员工列表表格 -->
        <table class="data-table">
            <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>部门</th>
                <th>职位</th>
                <th>薪资</th>
                <th>入职日期</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <%
                java.util.List<com.company.ems.model.Employee> employees =
                        (java.util.List<com.company.ems.model.Employee>) request.getAttribute("employees");
                if (employees != null && !employees.isEmpty()) {
                    for (com.company.ems.model.Employee emp : employees) {
            %>
            <tr>
                <td><%= emp.getId() %></td>
                <td><%= emp.getName() %></td>
                <td><%= emp.getDepartment() %></td>
                <td><%= emp.getPosition() %></td>
                <td><%= emp.getSalary() %></td>
                <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(emp.getHireDate()) %></td>
                <td class="actions">
                    <a href="employees?action=view&id=<%= emp.getId() %>" class="btn view">查看</a>
                    <a href="employees?action=edit&id=<%= emp.getId() %>" class="btn edit">编辑</a>
                    <a href="employees?action=delete&id=<%= emp.getId() %>" class="btn delete"
                       onclick="return confirm('确定要删除该员工吗?')">删除</a>
                </td>
            </tr>
            <%
                }
            } else {
            %>
            <tr>
                <td colspan="7" class="no-data">没有找到员工数据</td>
            </tr>
            <%
                }
            %>
            </tbody>
        </table>
    </div>
</main>

<footer>
    <p>&copy; 2023 员工信息管理系统 版权所有</p>
</footer>
</body>
</html>